<?php

namespace App\Observers;

use App\Models\Course;
use App\Models\Profile;
use App\Models\User;
use App\Models\UserCourse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class UserCourseObserver
{
    /**
     * Handle the user course "created" event.
     *
     * @param  \App\Models\UserCourse $userCourse
     * @return void
     */
    public function created(UserCourse $userCourse)
    {
        //
    }

    /**
     * Handle the user course "updated" event.
     *
     * @param  \App\Models\UserCourse $userCourse
     * @return void
     */
    public function updated(UserCourse $userCourse)
    {

        //完成时是自带completed_at字段，更新课程的所有技能
//        Log::info($userCourse->completed_at);
        if ($userCourse->completed_at) {
            $course = Course::find($userCourse->course_id)->load(['skills']);

            $user = User::find($userCourse->user_id)->load(['profile']);

            if ($course->skills) {
                /*
                 * 首先获取课程的对应技能
                 * 如果已经学了技能就加时间
                 * 如果没有就以当前课程时间为技能学时
                 * */
                $courseSkill = $course->skills;
                $userSkill = $user->skills;
                $courseSkillIds = $courseSkill->pluck('pivot.skill_id');
                $courseSkillCompletePercentage = $courseSkill->pluck('pivot.complete_percentage', 'pivot.skill_id');
                $userSkillIds = $userSkill->pluck('pivot.skill_id');//这些是学了技能的
                $intersectIds = $courseSkillIds->intersect($userSkillIds);
                $exceptIds = $courseSkillIds->diff($intersectIds);//这些是没学的
                $userSkillCompletePercentage = $userSkill->pluck('pivot.complete_percentage', 'pivot.skill_id');


                dd($courseSkillCompletePercentage, $userSkillCompletePercentage);

                //更新已经学了的

                DB::table('user_skill')->where('user_id', $userCourse->user_id)->whereIn('skill_id', $intersectIds)->increment('learn_count', $userCourse->learn_count);


                DB::table('user_skill')->where('user_id', $userCourse->user_id)->whereIn('skill_id', $intersectIds);


                $insertArr = $exceptIds->map(function ($item) use ($userCourse, $courseSkillCompletePercentage) {
                    return ['skill_id' => $item, 'user_id' => $userCourse->user_id, 'learn_count' => $userCourse->learn_count, 'complete_percentage' => $courseSkillCompletePercentage[$item]];
                });

                DB::table('user_skill')->insert($insertArr->toArray());
            }
        }
    }

    /**
     * Handle the user course "deleted" event.
     *
     * @param  \App\Models\UserCourse $userCourse
     * @return void
     */
    public
    function deleted(UserCourse $userCourse)
    {
        //
    }

    /**
     * Handle the user course "restored" event.
     *
     * @param  \App\Models\UserCourse $userCourse
     * @return void
     */
    public
    function restored(UserCourse $userCourse)
    {
        //
    }

    /**
     * Handle the user course "force deleted" event.
     *
     * @param  \App\Models\UserCourse $userCourse
     * @return void
     */
    public
    function forceDeleted(UserCourse $userCourse)
    {
        //
    }
}
